/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.pac4.only5.manager.impl;

import edu.uoc.tdp.pac4.only5.domain.Payment;
import edu.uoc.tdp.pac4.only5.exceptions.EespaisDatabaseException;
import edu.uoc.tdp.pac4.only5.manager.DatabaseConnectionManager;
import edu.uoc.tdp.pac4.only5.manager.PaymentManager;
import edu.uoc.tdp.pac4.only5.tools.DateTool;
import java.rmi.RemoteException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

/**
 *
 * @author Rafael Correal
 * ManangerImpl de Payment
 */
public class PaymentManagerImpl extends AbstractManager implements PaymentManager {

    public PaymentManagerImpl(DatabaseConnectionManager databaseConnectionManager) throws RemoteException {
        super(databaseConnectionManager);
    }

    @Override
    public Payment getPaymentbyId(int bookingId) throws RemoteException, EespaisDatabaseException {
        Payment payment = null;
        try {
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT * FROM payments WHERE bookingid = ?");
            getDatabaseConnectionManager().openConnection();
            PreparedStatement preparedStatement = getDatabaseConnectionManager().createPreparedStatement(sql.toString());
            preparedStatement.setInt(1, bookingId);
            preparedStatement.execute();
            getDatabaseConnectionManager().closeConnection();
            ResultSet resultSet = preparedStatement.getResultSet();
            while (resultSet.next()) {
                payment = getPayment(resultSet);
            }
        } catch (SQLException ex) {
            throw new EespaisDatabaseException(ex.getMessage(), ex);
        }

        return payment;
    }

    /*
     * @
     */
    public static Payment getPayment(ResultSet rs) throws SQLException {
        Payment pay = new Payment();
        pay.setDate_charged(rs.getString("payment_date"));
        pay.setOrder_id(rs.getInt("bookingid"));

        pay.setPrice(rs.getInt("total_price"));

        return pay;
    }
    /*
     * Updatea payments por bookingId
     * @param bookingId
     * @param precio
     *  
     * 
     */

    public void UpdatePaymentBooking(int bookingId, int precio) throws RemoteException {
        Date fecha;
        StringBuilder sql = new StringBuilder();
        fecha = DateTool.getInstance().getSqlDate(new Date());
        try {
            String sql1 = null;
            sql1 = "UPDATE payments  SET total_price = (total_price + " + precio + ")";
            sql1 = sql1 + " WHERE bookingid=" + bookingId;

            sql.append(sql1);

            getDatabaseConnectionManager().openConnection();
            PreparedStatement preparedStatement = getDatabaseConnectionManager().createPreparedStatement(sql.toString());
            preparedStatement.execute();

            getDatabaseConnectionManager().closeConnection();
            ResultSet resultSet = preparedStatement.getResultSet();
        } catch (EespaisDatabaseException ex) {
            //TODO handle errors!!!
            ex.printStackTrace();
        } catch (SQLException ex) {
            //TODO handle errors!!!
            ex.printStackTrace();
        }


    }

    @Override
    public void newPayment(Integer bookingId, Double precio) throws RemoteException, EespaisDatabaseException {
        try {
            String sql = "INSERT INTO payments (bookingid,payment_date,total_price,record_date) VALUES (?,?,?,?)";
            getDatabaseConnectionManager().openConnection();
            PreparedStatement preparedStatement = getDatabaseConnectionManager().createPreparedStatement(sql.toString());
            preparedStatement.setInt(1, bookingId);
            preparedStatement.setDate(2, DateTool.getInstance().getSqlDate(new Date()));
            preparedStatement.setDouble(3, precio);
            preparedStatement.setDate(4, DateTool.getInstance().getSqlDate(new Date()));
            preparedStatement.execute();
        } catch (SQLException ex) {
            throw new EespaisDatabaseException(ex.getMessage(), ex);
        }
    }
}
